{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "* 阶段：week10\n",
    "* 目标：实现web应用\n",
    "* 时间：2023-11-08\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周内容：Web应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 使用Flask 搭建Web最小应用\n",
    "\n",
    "> 1. 使用select创建一个html页面:index.html\n",
    "> 2. 使用Flsak中的 render_template 实现页面的路由跳转\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app '__main__'\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
      " * Running on http://127.0.0.1:5000\n",
      "Press CTRL+C to quit\n",
      "127.0.0.1 - - [15/Nov/2023 20:22:21] \"GET / HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [15/Nov/2023 20:22:24] \"POST /user_choose HTTP/1.1\" 404 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask  # 导入Flask库\n",
    "from flask import render_template\n",
    "\n",
    "app = Flask(__name__) # 创建一个Flask应用实例\n",
    "\n",
    "@app.route('/')\n",
    "def hello_world():\n",
    "    return render_template('index.html')\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 提交表单的内容在python中接收"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from flask import Flask\n",
    "from flask import render_template,request\n",
    "import crawl_liepin\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "@app.route('/')\n",
    "def index():\n",
    "    return render_template(\"index.html\")\n",
    "\n",
    "@app.route('/user_choose',methods = ['POST', 'GET'])\n",
    "def user_choose():\n",
    "    print(request.form['city'])\n",
    "    print(request.form['category'])\n",
    "    print(request.form['workexp'])\n",
    "    user_city = request.form['city']\n",
    "    user_category = request.form['category']\n",
    "    user_workexp = request.form['workexp']\n",
    "    crawl_results = crawl_liepin.crawl(城市 = user_city ,关键词=user_category,工作经验=user_workexp)\n",
    "    crawl_results_df = crawl_results[\n",
    "        [\n",
    "            'recruiter.recruiterTitle',\n",
    "            'comp.compScale',\n",
    "            'comp.compStage',\n",
    "            'comp.compIndustry',\n",
    "            'comp.compName',\n",
    "            'job.labels',\n",
    "            'job.dq',\n",
    "            'job.campusJobKind',\n",
    "            'job.title',\n",
    "            'job.salary'\n",
    "        ]\n",
    "    ]\n",
    "    crawl_results_df.to_excel('%s_%s_%s.xlsx'%(user_city,user_category,user_workexp))\n",
    "    crawl_results_html = crawl_results_df.to_html()   \n",
    "    # 文件存储\n",
    "    text_file = open(\"templates/results.html\", \"w\",encoding=\"utf-8\")\n",
    "    text_file.write(crawl_results_html)\n",
    "    text_file.close()\n",
    "    return render_template(\"results.html\")\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
